<template>
 <div  class="components-container">
     
    <sweet-modal ref="dialog" class="dialog">
      <h3>{{ title }}</h3>
  
      <div style=" height:200px; margin:30px 50px">
        <div class="layui-form-item">
          <div class="layui-inline">
              <label class="layui-form-label">登录名</label>
              <div class="layui-input-inline">
                  <el-input v-model="userData.USRLOGINNAME"   placeholder="请输入用户登录名"></el-input>
              </div>
              <label class="layui-form-label">用户名称</label>
              <div class="layui-input-inline">
                  <el-input v-model="userData.USRNAME"   placeholder="请输入用户名称"></el-input>
              </div>
          </div>
        </div>

        <div class="layui-form-item">
          <div class="layui-inline">
              <label class="layui-form-label">手机号</label>
              <div class="layui-input-inline">
                  <el-input  v-model="userData.USRPHONE"   placeholder="请输入手机号"></el-input>
              </div>
              <label class="layui-form-label">电话号码</label>
              <div class="layui-input-inline">
                  <el-input v-model="userData.USRTEL"  placeholder="请输入电话号码" ></el-input>
              </div>
          </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">部门编号</label>
                <div class="layui-input-inline">
                  <el-select v-model="userData.USRDPTID" placeholder="请选择部门编号" style="width:100%" >
                    <el-option  v-for="item in userDeptNos" :key="item.value" :label="item.label" :value="item.value"></el-option>
                  </el-select>
                </div>
                <label class="layui-form-label">角色选择</label>
                <div class="layui-input-inline">
                  
                  <el-select v-model="currentRols" multiple placeholder="请选择用户角色"  >
                      <el-option v-for="item in useRoleOptions" :key="item.value"  :label="item.label" :value="item.value"></el-option>


                      <!-- <template v-for="item in useRoleOptions">
                          <el-option :key="item.value"
                              :label="item.label"
                              :value="item.value"
                              v-if="item.label === currentUseRole" selected>
                          </el-option>
                          <el-option :key="item.value"
                              :label="item.label"
                              :value="item.value" v-else></el-option>
                      </template> -->
                  </el-select>
                </div>
            </div>
        </div>
      </div>

      <div class="layui-form-item btnCenter">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="submit" :loading="this.$store.state.isloading">确定</el-button>
      </div>
    </sweet-modal>

    <!-- 详情 -->
    <sweet-modal ref="dialogInfo" class="dialog">
          <h3>{{ title }}</h3>
          
             <div style=" height:200px; margin:30px 50px">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">登录名</label>
                        <div class="layui-input-inline">
                           <el-input v-model="userData.USRLOGINNAME"  readonly></el-input>
                        </div>
                        <label class="layui-form-label">用户名称</label>
                        <div class="layui-input-inline">
                           <el-input v-model="userData.USRNAME" readonly></el-input>
                       
                        </div>
                    </div>
                  </div>

                  <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-inline">

                           <el-input  v-model="userData.USRPHONE" readonly></el-input>
                       
                        </div>
                        <label class="layui-form-label">电话号码</label>
                        <div class="layui-input-inline">
                           <el-input v-model="userData.USRTEL" readonly></el-input>
                        
                        </div>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">部门编号</label>
                        <div class="layui-input-inline">

                            <el-input  v-model="userData.DPTNAME" readonly></el-input>
                         
                        </div>
                        <label class="layui-form-label">角色</label>
                        <div class="layui-input-inline">
                            <el-input   type="textarea"  v-model="roles" readonly></el-input>
                     
                        </div>
                    </div>
                  </div>
             </div>

          <div class="layui-form-item btnCenter">
              <el-button @click="closeDialog">取消</el-button>
          </div>
    </sweet-modal>
 </div>
</template>

<script>
import sweetModal from "@/components/sweetModal";
import apiBase from "@/api/apiBase.js";
const ADD = "新增";
const MODIFY = "修改";
const DELETE = "删除";
const INFO = "详情";
export default {
  data() {
    return {
     
      title: null,
      useRoleOptions: [],
      btnListIfon: [],
      currentRols: [],
      userDeptNos: [],
      
      userData: {
        USRLOGINNAME:null,
        USRNAME:null,
        USRPHONE:null,
        USRTEL:null,
        USRDPTID:null,
        Rols:[],
      },
      roles:null
      
     }
   },
   methods: {
      //功能操作
      open(order, id) {
        this.order = order;
        this.getUserDept()
        this.getRoles();
        switch (this.order) {
          case ADD:
            this.title = "新增";
             this.dataRepeat();
            this.$refs.dialog.open();
            break;
          case MODIFY:
            this.title = "修改";
          
            this.dataInfo(id);
            this.$refs.dialog.open();
            break;
          case INFO:
            this.title = "详情";
            this.dataInfo(id);
            this.$refs.dialogInfo.open();
            break;
          case DELETE:
            this.dataDelete(id);
            break;
          default:
        }
      },
    //数据重置
    dataRepeat() {
      this.userData = {}
      this.currentRols=[]
    },
    //提交
    submit() {
      this.$store.state.isloading = true;
      switch (this.order) {  
        case ADD:
          this.dataAdd();
          break;
        case MODIFY:
          this.dataModify();
          break;
        case DELETE:
          break;
        case INFO:
          this.$refs.dialog.close();
          break;
        default:
      }
    },

    getRoles() {
      var data = {};
      apiBase.DoService(data, apiBase.getApiAdress().User.QueryRole, res => {
        let userRoleData = res.data.content;
       
         var legendsUsersData = userRoleData.map(item => ({
            label: item.ROLNAME,
            value: item.ROLID
          }));
        this.useRoleOptions = legendsUsersData;
        //console.log('userRoleOpt',this.useRoleOptions)
      });
    },
    //部门查询
    getUserDept() {
      var data = {};
      apiBase.DoService(data, apiBase.getApiAdress().User.QueryDept, res => {
        let userDptData = res.data.content;
        var  legendsDptsData = userDptData.map(item => ({
            label: item.DPTNAME,
            value: item.DPTID
          }));
        this.userDeptNos = legendsDptsData;
       
     
      });
    },
    //修改
    dataModify() {
     this.userData.Rols=this.currentRols;
      let Data = JSON.parse(JSON.stringify( this.userData ));
      apiBase.DoService(Data, apiBase.getApiAdress().User.Modify, res => {
       
        this.$refs.dialog.close();
        this.refreshData();
        this.$message({
          type: "success",
          message: "修改成功!"
        });
        this.refreshData();
      }, res =>{
        this.$message.error(res.data.msg);
      
      
      });
    },
    //新增提交
    dataAdd() {
     
      this.userData.Rols=this.currentRols;
      let Data = JSON.parse(JSON.stringify(this.userData));
      apiBase.DoService(Data, apiBase.getApiAdress().User.Create, res => {
       
         this.$refs.dialog.close();
        this.refreshData();
        this.$message({
          type: "success",
          message: "新增成功!"
        });
       
    
      }, res =>{
        this.$message.error(res.data.msg);
      });
    },
    //详情
    dataInfo(id) {
      let data = {
        USRID: id
      };

      apiBase.DoService(data, apiBase.getApiAdress().User.Detail, res => {
        
        this.userData = res.data.content;
       
         if (
          !this.userDeptNos.some(item => {
            return item.value === this.userData.USRDPTID;
          })
          )
          this.userDeptNos.push({
            label: this.userData.DPTNAME,
            value: this.userData.USRDPTID
          });
        
        let userRoles=[];
        this.currentRols=[];
        //this.currentRols
        let legendsModifyData = res.data.content.Rols.map(item => ({
            label: item.ROLNAME,
            value: item.ROLID
          }));
        
        legendsModifyData.forEach((item, index) => {
         userRoles.push(item.label);
         this.currentRols.push(item.value);
         
        })
      
        this.roles=userRoles.join(',')
    
        
      });
    },
    // 删除表格列
    dataDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            USRID: id
          };
          apiBase.DoService(data, apiBase.getApiAdress().User.Delete, res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.refreshData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
      },
      refreshData() {
      this.$emit("refreshData");
    },
      closeDialog() {
        if (this.order === ADD || this.order === MODIFY)
          this.$refs.dialog.close();
        else if (this.order === INFO) this.$refs.dialogInfo.close();
      },
   },
   components: {
       sweetModal
   }
 }
</script>

<style>

</style>
